﻿using CSRedis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AIStudio.ConSole.Redis.Ch03
{
    /********************************************************************************************************
    # <start id="simple-pipeline-trans"/>
    >>> def trans():
    ...     pipeline = conn.pipeline()                      #A
    ...     pipeline.incr('trans:')                         #B
    ...     time.sleep(.1)                                  #C
    ...     pipeline.incr('trans:', -1)                     #D
    ...     print pipeline.execute()[0]                     #E
    ...
    >>> if 1:
    ...     for i in xrange(3):                             #F
    ...         threading.Thread(target=trans).start()      #F
    ...     time.sleep(.5)                                  #G
    ...
    1                                                       #H
    1                                                       #H
    1                                                       #H
    # <end id="simple-pipeline-trans"/>
    *********************************************************************************************************/
    class TestTrans
    {
        CSRedisClient _conn;
        public TestTrans()
        {
            _conn = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=14,poolsize=500,ssl=false,writeBuffer=10240");
        }
        public void Trans()
        {
            var pipeline = _conn.StartPipe();
            Console.WriteLine(pipeline.IncrBy("notrans:"));
            Thread.Sleep(100);
            pipeline.IncrBy("notrans:", -1);
            Console.WriteLine(pipeline.EndPipe()[0]);
        }

        public void Test()
        {
            for (int i = 0; i < 3; i++)
            {
                Thread thread = new Thread(new ThreadStart(Trans));
                thread.Start();
            }
            Thread.Sleep(500);
        }
    }
}
